package com.jpcc.vw.onlineRadio.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ConfigReaderUtils {

    public static String readDidFromConfig() {
        File onlineConfig = null;
        String filepath = "/mnt/nfs/onlineapplication.conf";
        onlineConfig = new File(filepath);

        String str = null;
        String sVin = "";
        String sUid = "";
        String sDid = "";
        try {
            InputStream is = new FileInputStream(onlineConfig);
            InputStreamReader input = new InputStreamReader(is, "UTF-8");
            BufferedReader reader = new BufferedReader(input);
            while ((str = reader.readLine()) != null) {

                if(str.indexOf("device:") != -1 ){
                    sDid = str.substring(str.indexOf("device:")+7,str.length());
                }

                if(str.indexOf("user:") != -1 ){
                    sUid = str.substring(str.indexOf("user:")+5,str.length());
                }
                if(str.indexOf("vin:") != -1 ){
                    sVin = str.substring(str.indexOf("vin:")+4,str.length());
                }
            }
            reader.close();
            return sDid;
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }


    public static String readUidFromConfig() {
        File onlineConfig = null;
        String filepath = "/mnt/nfs/onlineapplication.conf";
        onlineConfig = new File(filepath);

        String str = null;
        String sVin = "";
        String sUid = "";
        String sDid = "";
        try {
            InputStream is = new FileInputStream(onlineConfig);
            InputStreamReader input = new InputStreamReader(is, "UTF-8");
            BufferedReader reader = new BufferedReader(input);
            while ((str = reader.readLine()) != null) {

                if(str.indexOf("device:") != -1 ){
                    sDid = str.substring(str.indexOf("device:")+7,str.length());
                }

                if(str.indexOf("user:") != -1 ){
                    sUid = str.substring(str.indexOf("user:")+5,str.length());
                }
                if(str.indexOf("vin:") != -1 ){
                    sVin = str.substring(str.indexOf("vin:")+4,str.length());
                }
            }
            reader.close();
            return sUid;
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }


    public static String readVidFromConfig() {
        File onlineConfig = null;
        String filepath = "/mnt/nfs/onlineapplication.conf";
        onlineConfig = new File(filepath);

        String str = null;
        String sVin = "";
        String sUid = "";
        String sDid = "";
        try {
            InputStream is = new FileInputStream(onlineConfig);
            InputStreamReader input = new InputStreamReader(is, "UTF-8");
            BufferedReader reader = new BufferedReader(input);
            while ((str = reader.readLine()) != null) {

                if(str.indexOf("device:") != -1 ){
                    sDid = str.substring(str.indexOf("device:")+7,str.length());
                }

                if(str.indexOf("user:") != -1 ){
                    sUid = str.substring(str.indexOf("user:")+5,str.length());
                }
                if(str.indexOf("vin:") != -1 ){
                    sVin = str.substring(str.indexOf("vin:")+4,str.length());
                }
            }
            reader.close();
            return sVin;
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }
}
